import { Module } from "vuex";
import { State } from "..";
import http from "../../api/request";
import { Todo } from "../../types";

/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-12-09 05:47:17
 * @LastEditors: GGB
 * @LastEditTime: 2021-12-09 07:19:32
 */
const initialState = {
    todos: [] as Todo[],
}
export type TodoState = typeof initialState;
export default {
    namespaced: true,
    state: initialState,
    mutations: {
        initTodo(state, payload: Todo[]) {
            state.todos = payload
        },
        addTodo(state, payload: Todo) {
            state.todos.push(payload)
        }
    },
    actions: {
        initTodo({ commit }) {
            // http.get<Todo>("https://jsonplaceholder.typicode.com/todos/1").then(res => {
            http.get<Todo>("/todos/1").then(res => {
                commit('addTodo', res.data)
                console.log(res.data)
            })
            // setTimeout(() => {
            //     commit('initTodo', [{
            //         id: 1,
            //         name: "vue3+ts",
            //         complated: false
            //     }])
            // })
        },
        addTodo({ commit, state }, payload: string) {
            commit('addTodo', {
                id: state.todos.length,
                title: payload,
                complated: false
            } as Todo)
        }
    }
} as Module<TodoState, State>